<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="shortcut icon" href="../static/icon/Bell.ico" />
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      ul {
        margin: 100px auto;
        list-style: none;
        width: 300px;
        height: 40px;
      }
      ul li {
        float: left;
        width: 48px;
        height: 38px;
        line-height: 40px;
        text-align: center;
        margin-left: 20px;
        border: 1px solid orange;
        background-color: #bfa;
        cursor: pointer;
        color: darkgray;
        border-radius: 10px 10px;
      }
      .focus {
        background-color: #77a;
      }
      #show {
        width: 480px;
        height: 320px;
        margin: 150px auto;
        border: 1px solid inherit;
      }
    </style>
  </head>
  <body>
    <ul id="tab">
      <li class="focus">图片1</li>
      <li>图片2</li>
      <li>图片3</li>
      <li>图片4</li>
    </ul>
    <div id="show"><img src="../static/img/01.jpg" alt="" /></div>
  </body>
  <script>
    window.onload = function () {
      let lis = document.getElementById("tab").getElementsByTagName("li");
      let img = document.getElementById("show").getElementsByTagName("img")[0];
      for (let i = 0; i < lis.length; ++i) {
        lis[i].setAttribute("index", i); // 为每一个li设置索引
      }
      for (let li of lis) {
        li.onclick = function () {
          li.setAttribute("class", "focus");
          let index = li.getAttribute("index"); // 为当前点击的li添加focus class
          for (let i = 0; i < lis.length; ++i) {
            if (index !== i) {
              lis[i].removeAttribute("class"); // 移除没有被选中的li的class
            }
          }

          img.setAttribute("src", "../static/img/0" + (++index) + ".jpg");
          // div.appendChild(img);
        };
      }
    };
  </script>
</html>
